package br.net.woodstock.tickettoride.api.struts2.core.impl;

import java.util.Collection;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.net.woodstock.rockframework.persistence.orm.QueryMetadata;
import br.net.woodstock.rockframework.persistence.orm.QueryResult;
import br.net.woodstock.rockframework.persistence.orm.impl.JPAQueryableRepository;
import br.net.woodstock.rockframework.persistence.orm.impl.QueryMetadataImpl;
import br.net.woodstock.tickettoride.api.core.SimpleQueryableRepository;

@Repository
public class SimpleQueryableRepositoryImpl implements SimpleQueryableRepository {

	private static final long	serialVersionUID	= -3139155797801102518L;

	@PersistenceContext(name = "defaultPU")
	private EntityManager		entityManager;

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public <E> E getSingle(final String jpql, final Map<String, Object> parameters) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, null);
		return this.getSingle(metadata);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public <E> E getSingle(final String jpql, final Map<String, Object> parameters, final Map<String, Object> options) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, options);
		return this.getSingle(metadata);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public <E> Collection<E> getCollection(final String jpql, final Map<String, Object> parameters) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, null);
		return this.getCollection(metadata).getResult();
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public <E> Collection<E> getCollection(final String jpql, final Map<String, Object> parameters, final Map<String, Object> options) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, options);
		return this.getCollection(metadata).getResult();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void executeUpdate(final String jpql, final Map<String, Object> parameters) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, null);
		this.executeUpdate(metadata);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void executeUpdate(final String jpql, final Map<String, Object> parameters, final Map<String, Object> options) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, options);
		this.executeUpdate(metadata);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public <E> E getSingle(final QueryMetadata query) {
		return new JPAQueryableRepository(this.entityManager).getSingle(query);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public QueryResult getCollection(final QueryMetadata query) {
		return new JPAQueryableRepository(this.entityManager).getCollection(query);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void executeUpdate(final QueryMetadata query) {
		new JPAQueryableRepository(this.entityManager).executeUpdate(query);
	}

}